package com.cpi.cornpp.balanceversion.converter;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.cpi.cornpp.balanceversion.dto.BalanceVersionDTO;
import com.cpi.cornpp.balanceversion.exception.BalanceVersionException;
import com.cpi.cornpp.balanceversion.view.BalanceVersionView;
import com.cpi.cornpp.common.exception.CornPPException;
import com.cs.common.utils.NumberUtil;

/**
 * Balance Version Converter
 * @author Edgar Joao
 *
 */
public class BalanceVersionConverter {		
	private static double ZERO = 0.00;
	/**
	 * Balance Version DTO to Views Converter
	 * @author Edgar Joao
	 * @param dto
	 * @return
	 * @throws BalanceVersionException
	 */
	public BalanceVersionView converterBalanceVersionDtoToView(BalanceVersionDTO dto) throws BalanceVersionException{
		BalanceVersionView balanceVersionView = null;
		String amountWithOutOfCredit = null;
		try{
			if(dto != null){
				balanceVersionView = new BalanceVersionView();
				balanceVersionView.setMonthPeriodVersionId(dto.getMonthPeriodVersionId());
				balanceVersionView.setBalanceVersionId(dto.getBalanceVersionId());
				balanceVersionView.setAmount(NumberUtil.convertPrice(dto.getAmount()));
				balanceVersionView.setOutOfCredit(NumberUtil.convertQuantity(dto.getOutOfCredit() * 100));
				balanceVersionView.setLibor(NumberUtil.convertQuantity(dto.getTax1() * 100));
				balanceVersionView.setSpread(NumberUtil.convertQuantity(dto.getTax2() * 100));
				balanceVersionView.setBalance(NumberUtil.convertPrice(dto.getBalance()));
				amountWithOutOfCredit = NumberUtil.convertPrice(dto.getAmount() * (1+dto.getOutOfCredit()));
				balanceVersionView.setAmountWithOutOfCredit(amountWithOutOfCredit);
				balanceVersionView.setBalanceVersion(NumberUtil.convertPrice(dto.getBalanceVersion()));
				balanceVersionView.setBalanceTotal(NumberUtil.convertPrice(dto.getBalanceVersion()+ dto.getBalance()));
			}
		}catch( Exception exception ){
			BalanceVersionException balanceVersionException=null;
			exception.printStackTrace(System.out);
			balanceVersionException= new BalanceVersionException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
			throw balanceVersionException;
		}
		return balanceVersionView;
	}
	/**
	 * @author Edgar Joao
	 * @param balansVersionsDTOs
	 * @return
	 * @throws BalanceVersionException
	 */
	public List convertDTOsToViews(List balansVersionsDTOs) throws BalanceVersionException{
		List listOfBalansVersionsDTOs = null;
		Iterator iterator = null;
		BalanceVersionDTO balanceVersionDTO = null;
		BalanceVersionView balanceVersionView = null;
		try{
			listOfBalansVersionsDTOs = new ArrayList(0);
			iterator = balansVersionsDTOs.iterator();
			while( iterator.hasNext() ){
				balanceVersionDTO =  (BalanceVersionDTO) iterator.next();
				balanceVersionView = converterBalanceVersionDtoToView(balanceVersionDTO);
				listOfBalansVersionsDTOs.add(balanceVersionView);
			}						
		}catch(BalanceVersionException balanceVersionException){
			throw balanceVersionException;
		}catch( Exception exception ){
			BalanceVersionException balanceVersionException=null;
			exception.printStackTrace(System.out);
			balanceVersionException= new BalanceVersionException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
			throw balanceVersionException;
		}
		return listOfBalansVersionsDTOs;
	}
	/**
	 * @author Edgar Joao
	 * @param view
	 * @return
	 * @throws BalanceVersionException
	 */
	public BalanceVersionDTO converterBalanceVersionViewToDto(BalanceVersionView view) throws BalanceVersionException{
		BalanceVersionDTO balanceVersionDTO = null;
		try{
			if(view != null){
				balanceVersionDTO = new BalanceVersionDTO();
				balanceVersionDTO.setBalanceVersionId(view.getBalanceVersionId());
				if(StringUtils.equals(StringUtils.EMPTY, view.getAmount().trim())){
					balanceVersionDTO.setAmount(ZERO);	
				}else{
					balanceVersionDTO.setAmount(NumberUtil.parseDouble(view.getAmount()));
				}
				if(StringUtils.equals(StringUtils.EMPTY, view.getBalance().trim())){
					balanceVersionDTO.setBalance(ZERO);	
				}else{
					balanceVersionDTO.setBalance(NumberUtil.parseDouble(view.getBalance()));
				}
				if(StringUtils.equals(StringUtils.EMPTY, view.getOutOfCredit().trim())){
					balanceVersionDTO.setOutOfCredit(ZERO);	
				}else{
					balanceVersionDTO.setOutOfCredit(NumberUtil.parseDouble(view.getOutOfCredit())/100);
				}
				if(StringUtils.equals(StringUtils.EMPTY, view.getLibor().trim())){
					balanceVersionDTO.setTax1(ZERO);	
				}else{
					balanceVersionDTO.setTax1(NumberUtil.parseDouble(view.getLibor())/100);
				}
				if(StringUtils.equals(StringUtils.EMPTY, view.getSpread().trim())){
					balanceVersionDTO.setTax2(ZERO);	
				}else{
					balanceVersionDTO.setTax2(NumberUtil.parseDouble(view.getSpread())/100);
				}
			}			
		}catch( Exception exception ){
			BalanceVersionException balanceVersionException=null;
			exception.printStackTrace(System.out);
			balanceVersionException= new BalanceVersionException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
			throw balanceVersionException;
		}
		return balanceVersionDTO;
	}
}
